$(function() {

	var searchResultsEvents = function() {
		$('.propositions .d_propositions div.service_choose div.row').mouseover(function() {
			var $this = $(this);
			if (!$this.hasClass('selected')) {
				$this.addClass('hovered');
			}
		});
		
		$('div.service_choose div.row').mouseout(function() {
			$(this).removeClass('hovered');
		}).mousedown(function() {
			var $this = $(this);
			
			var vc = $this.parents('.service_choose').find('div.selected input.vendor-code').val();
			var rc = $this.parents('.service_choose').find('div.selected input.room-code').val();
			var vendor = (vc == 'hotelbeds' || vc.match(/hotelspro/)) ? true : false;
			var roomCode = $this.children('input.room-code').val();
			var thisVC = $this.find('input.vendor-code').val();

			if ($this.parents('.service_choose').find('input.vendor-sync').length > 0) {
				if (thisVC != 'hotelbeds' /*&& thisVC.match(/hotelspro/) === null*/ && vendor) {
					var mustBeSelected = $this.parents('div.service_choose').siblings('div.service_choose').filter(function() {
						return $(this).find('input.vendor-sync').length > 0;
					}).find('div.row').filter(function() {
						var c = $(this).find('input.vendor-code').val();
						return (c != 'hotelbeds' && c.match(/hotelspro/) == null);
					});
					
					mustBeSelected.addClass('mustBeSelected');
					mustBeSelected = mustBeSelected.parents('div.service_choose').find('.mustBeSelected:first');
					
					mustBeSelected.parents('div.service_choose').find('div.row').removeClass('selected');
					mustBeSelected.parents('div.service_choose').find('a.tm-radiobutton img').removeClass('checked');
					mustBeSelected.find('input[type="radio"]').trigger('click').next(0).find('img').addClass('checked');
					mustBeSelected.addClass('selected').removeClass('hovered');
				}
			}

			var vendorSyncInp = '<input type="hidden" disabled="disabled" class="vendor-sync" />';
			$this.parents('div.service_choose').siblings('div.service_choose').find('div.row > input.room-code').each(function(i) {
				var
					inpRoomCode = $(this), 
					inpRoomCodeServiceChoose = inpRoomCode.parents('div.service_choose');
					$thisServiceChoose = $this.parents('div.service_choose')
				;
				if (inpRoomCode.val() == roomCode) {
					inpRoomCodeServiceChoose.find('div.selected').removeClass('selected');
					inpRoomCodeServiceChoose.find('a.tm-radiobutton img').removeClass('checked');
					inpRoomCode.parents('div.row').find('input[type="radio"]').trigger('click').next(0).find('img').addClass('checked');
					inpRoomCode.parents('div.row').addClass('selected').removeClass('hovered');
					
					if (inpRoomCodeServiceChoose.find('input.vendor-sync').length <= 0) {
						inpRoomCodeServiceChoose.append(vendorSyncInp);
					}
					if ($thisServiceChoose.find('input.vendor-sync').length <= 0) {
						$thisServiceChoose.append(vendorSyncInp);
					}
				}
			});

			
			$this.parents('.service_choose').find('div.selected').removeClass('selected');
			$this.parents('.service_choose').find('a.tm-radiobutton img').removeClass('checked');
			$this.find('input[type="radio"]').trigger('click').next(0).find('img').addClass('checked');
			$this.addClass('selected');
			recalculatePrice(this);
			$(this).removeClass('hovered');
		});
		
		var recalculatePrice = function(elm) {
			var roomPrice, cashType, transferPrice, summary, summaryPrice;
			var roomPrice = [];
			var roomPriceAll = 0;
			
			summary = $(elm).parents('form.propositions').find('.search_result_bottom .summary .summ');
			
			$(elm).parents('form.propositions').find('.room_choose').each(function() {
				var $this = $(this);
				var prc;
				prc = $this.find('.row.selected .price').length > 0 ? $this.find('.row.selected .price').html().replace(/(^\s+)|(\s+$)/gi, '') : '0';
				roomPrice.push(prc);
			});
			
			if ($(elm).parents('form.propositions').find('.transfer_choose').length > 0) {
				transferPrice = $(elm).parents('form.propositions').find('.transfer_choose .selected .price').html().replace(/(^\s+)|(\s+$)/gi, '');
			} else {
				transferPrice = '';
			}

			for (var j = 0; j < roomPrice.length; j++) {
				if (roomPrice[j].length > 1 && roomPrice[j] != null) {
					cashType = roomPrice[j].match(/(^\S)/gi);
					break;
				} else if (transferPrice.length > 1 && transferPrice != null) {
					cashType = transferPrice.match(/(^\S)/gi);
					break;
				} else {
					cashType = '';
					break;
				}
			}

			for (var i = 0; i < roomPrice.length; i++) {
				roomPrice[i] = (roomPrice[i].length > 1 && roomPrice[i] != null) ? roomPrice[i].replace(/(^\S)/gi, '') : '0';
				roomPriceAll += parseInt(roomPrice[i]);
			}
			transferPrice = (transferPrice.length > 1 && transferPrice != null) ? transferPrice.replace(/(^\S)/gi, '') : '0' ;

			summaryPrice = roomPriceAll + parseInt(transferPrice);
			summary.text(cashType + summaryPrice);
		};
		
		$('div.service_choose div.row').each(function() {
			recalculatePrice(this);
		});
		
	}
	
	searchResultsEvents();
	
	$('a.propose_show').filter(function(i){
		var v = $(this).metadata().expandHotel;
		return (v != undefined && v.length > 0);
	}).eq(0).trigger('click');
	
});